package com.yshye.adapter.wiki;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

public class LogFIleOpeate implements LogFileOperateApi {
	private String logFIlePathName = "Log.log";

	public LogFIleOpeate(String logFIlePathName) {
		if (logFIlePathName != null && logFIlePathName.trim().length() > 0) {
			this.logFIlePathName = logFIlePathName;
		}
	}

	public List<LogModel> readLogFile() {
		List<LogModel> list = null;
		ObjectInputStream oin = null;

		try {
			File file = new File(logFIlePathName);
			if (file.exists()) {
				oin = new ObjectInputStream(new BufferedInputStream(
						new FileInputStream(file)));
				list = (List<LogModel>) oin.readObject();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (oin != null) {
					oin.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		return list;
	}

	public void writeLogFile(List<LogModel> list) {
		File f = new File(logFIlePathName);
		ObjectOutputStream oout = null;
		try {
			oout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
			oout.writeObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				oout.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}
